package org.rsbot.script.randoms;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.logging.Logger;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Settings;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSPlayer;

@ScriptManifest(authors={"Pwnaz0r", "Velocity"}, name="BeeHive", version=2.5D)
public class BeehiveSolver extends Random
{
  RSNPC BeehiveKeeper;
  private static final int BEEHIVE_KEEPER_ID = 8649;
  private static final int[] DEST_INTERFACE_IDS = { 16, 17, 18, 19 };
  private static final int ID_DOWN = 16034;
  private static final int ID_MIDDOWN = 16022;
  private static final int ID_MIDUP = 16025;
  private static final int ID_TOP = 16036;
  private static final int[] BEEHIVE_ARRAYS = { 16036, 16025, 16022, 16034 };
  private static final String[] MODEL_NAMES = { "Top", "Middle Up", "Middle Down", "Down" };
  boolean solved;
  private static final int[] START_INTERFACE_IDS = { 12, 13, 14, 15 };
  private static final int INTERFACE_BEEHIVE_WINDOW = 420;
  private static final int BUILD_BEEHIVE = 40;
  private static final int CLOSE_WINDOW = 38;

  public boolean activateCondition()
  {
    if (!this.game.isLoggedIn()) {
      return false;
    }
    if (this.npcs.getNearest(new int[] { 8649 }) != null) if (this.objects.getNearest(new int[] { 16168 }) != null) {
        this.solved = false;
        return true;
      }


    return false;
  }

  public boolean dragInterfaces(RSComponent paramRSComponent1, RSComponent paramRSComponent2) {
    Point localPoint1 = returnMidInterface(paramRSComponent1);
    Point localPoint2 = returnMidInterface(paramRSComponent2);

    this.mouse.move(localPoint1);
    this.mouse.drag(localPoint2);
    return true;
  }

  public RSInterface getBeehiveInterface() {
    return this.interfaces.get(420);
  }

  public int loop()
  {
    this.BeehiveKeeper = this.npcs.getNearest(new int[] { 8649 });
    if (this.BeehiveKeeper == null)
    {
      return -1;
    }

    if (myClickContinue()) {
      return 200;
    }

    if (this.interfaces.getComponent(236, 2).doClick()) {
      return random(800, 1200);
    }

    if (getBeehiveInterface().isValid()) {
      for (int i = 1; i < 5; i++) {
        this.log.info("Checking ID: " + i);
        int j = returnIdAtSlot(i);
        dragInterfaces(getBeehiveInterface().getComponent(START_INTERFACE_IDS[(i - 1)]), getBeehiveInterface().getComponent(returnDragTo(j)));
      }
      sleep(2000);

      if (this.settings.getSetting(805) == 109907968) {
        this.solved = true;
        log("All bee pieces have been place, now finishing random");
      } else {
        this.interfaces.getComponent(420, 38).doClick();
        return random(500, 1000);
      }
      if ((this.solved) && (this.interfaces.getComponent(420, 40).doClick()))
        return random(900, 1600);
    }
    else {
      this.log.info("Interfaces not valid.");
    }

    if (getMyPlayer().getInteracting() == null) {
      RSNPC localRSNPC = this.npcs.getNearest(new int[] { 8649 });
      if ((localRSNPC != null) && 
        (!localRSNPC.doAction("Talk-to"))) {
        this.camera.setAngle(this.camera.getAngle() + random(-30, 30));
      }

    }

    return random(500, 1000);
  }

  public boolean myClickContinue() {
    sleep(random(800, 1000));
    return (this.interfaces.getComponent(243, 7).doClick()) || (this.interfaces.getComponent(241, 5).doClick()) || (this.interfaces.getComponent(242, 6).doClick()) || (this.interfaces.getComponent(244, 8).doClick()) || (this.interfaces.getComponent(64, 5).doClick());
  }

  public int returnDragTo(int paramInt) {
    switch (paramInt) {
    case 16036:
      return DEST_INTERFACE_IDS[0];
    case 16025:
      return DEST_INTERFACE_IDS[1];
    case 16022:
      return DEST_INTERFACE_IDS[2];
    case 16034:
      return DEST_INTERFACE_IDS[3];
    }
    return -1;
  }

  public int returnIdAtSlot(int paramInt)
  {
    if ((paramInt < 1) || (paramInt > 4)) {
      this.log.info("Invalid Slot.");
      this.interfaces.getComponent(420, 38).doClick();
    }

    int i = getBeehiveInterface().getComponent(returnSlotId(paramInt)).getModelID();

    if (i == -1) {
      this.log.info("Could not retrieve ID. Restarting.");
      this.interfaces.getComponent(420, 38).doClick();
    }

    for (int j = 0; j < BEEHIVE_ARRAYS.length; j++) {
      if (i == BEEHIVE_ARRAYS[j]) {
        this.log.info("Slot " + paramInt + " contains section: " + MODEL_NAMES[j]);
        return i;
      }
    }

    return -1;
  }

  public Point returnMidInterface(RSComponent paramRSComponent) {
    Point localPoint = new Point(-1, -1);
    Rectangle localRectangle = paramRSComponent.getArea();
    if (localRectangle != null) {
      localPoint = new Point((int)localRectangle.getCenterX(), (int)localRectangle.getCenterY());
    }
    return localPoint;
  }

  public int returnSlotId(int paramInt) {
    switch (paramInt) {
    case 1:
      return 25;
    case 2:
      return 22;
    case 3:
      return 23;
    case 4:
      return 21;
    }
    this.log.info("Invalid slot ID. Restarting.");
    this.interfaces.getComponent(420, 38).doClick();

    return -1;
  }
}